package me.linczh.framework.filter;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import me.linczh.bank.dao.DaoFactory;
import me.linczh.bank.statistic.DailyTimerTask;
import me.linczh.framework.action.ActionMapping;
import me.linczh.framework.persiste.DBStoreHolder;
import me.linczh.framework.persiste.FileStoreHolder;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class FrameworkEntranceFilter implements Filter  {
	private ActionMapping mapping;
	private Timer timer;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("FrameworkFilterChainProxy#destory");
		timer.cancel();
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("FrameworkFilterChainProxy#doFilter");
		FilterChain frameworkChain = new FrameworkFilterChain(chain);
		frameworkChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("FrameworkFilterChainProxy#init");
		
		SAXBuilder builder = new SAXBuilder();
		Document actionConfigDocument = null;
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("actions.cfg.xml");
		try {
			actionConfigDocument = builder.build(in);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		mapping = ActionMapping.getInstance();
		mapping.initActionsMapping(actionConfigDocument);
		
		in = this.getClass().getClassLoader().getResourceAsStream("config.properties");
		Properties prop = new Properties();
		try {
			prop.load(in);
			DaoFactory.STORE = prop.getProperty("MODE");
			if (DaoFactory.STORE.equals("FILE")) {
				String classpath = this.getClass().getClassLoader().getResource("").toString();
				FileStoreHolder.WEBROOT_PATH = classpath.replaceFirst("WEB-INF/classes/", "");
				System.out.println(FileStoreHolder.WEBROOT_PATH);
				FileStoreHolder.loadAllRecords();
			} else {
				DBStoreHolder.URL = prop.getProperty("URL");
				DBStoreHolder.USERNAME = prop.getProperty("USERNAME");
				DBStoreHolder.PASSWORD = prop.getProperty("PASSWORD");
				DBStoreHolder.init();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		timer = new Timer();
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 1);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		Date standard = c.getTime();
		if (standard.before(new Date())) c.add(Calendar.DATE, 1);
		System.out.println("next timer will be at: " + c.getTime());
		timer.scheduleAtFixedRate(new DailyTimerTask(), c.getTime(), 1000 * 60 * 60 * 24);
	}
}
